cmake_minimum_required(VERSION 2.8)

project(mainapp)

#  C compiler
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
	set(USING_GCC 1)
	message("-- C:Compiler: gcc")
endif()

if(CMAKE_C_COMPILER_ID MATCHES "Clang")
  	set(USING_CLANG 1)
	message("-- C:Compiler: clang")
endif()

if(CMAKE_C_COMPILER_ID MATCHES "AppleClang")
  	set(USING_APPLECLANG 1)
	message("-- C:Compiler: apple_clang")
endif()

if(CMAKE_C_COMPILER_ID MATCHES "TI")
  	set(USING_TI 1)
	message("-- C:Compiler: ti")
endif()

if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
  	set(USING_MSVC 1)
  	message("-- C:Compiler: MSVC")
endif()


#CPP compiler
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
	set(USING_GCC 1)
	message("-- C:Compiler: g++")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  	set(USING_CLANG 1)
	message("-- C:Compiler: clang")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
  	set(USING_APPLECLANG 1)
	message("-- C:Compiler: apple_clang")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "TI")
  	set(USING_TI 1)
	message("-- C:Compiler: ti")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  	set(USING_MSVC 1)
  	message("-- C:Compiler: MSVC")
endif()

if (CMAKE_SYSTEM_PROCESSOR MATCHES "armv5te")
	set(Target32Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "armv7-a")
	add_definitions("-DTARGET32BIT")
	set(Target32Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "mips")
	add_definitions("-DTARGET32BIT")
	set(Target32Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "i686")
	add_definitions("-DTARGET32BIT")
	set(Target32Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
	add_definitions("-DTARGET32BIT")
	set(Target32Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
	add_definitions("-DTARGET64BIT")
	set(Target64Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "mips64")
	add_definitions("-DTARGET64BIT")
	set(Target64Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
	add_definitions("-DTARGET64BIT")
	set(Target64Bit true)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")
	add_definitions("-DTARGET64BIT")
	set(Target64Bit true)
else()
	message(FATAL_ERROR "Unknown Process Bit=${CMAKE_SYSTEM_PROCESSOR}")
endif()


if (CMAKE_SYSTEM_NAME MATCHES "Android")
	add_definitions("-DNDEBUG")
	add_definitions("-DANDROID")
	set(UNIX_OS true)
	set(ANDROID_OS true)
	message("-- ANDROID Cpu: ${ANDROID_ABI}")
	set(compile_os "Android")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
	add_definitions("-DLINUX")
	set(UNIX_OS true)
	set(compile_os "Linux")
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
	add_definitions("-DUNIX")
	set(UNIX_OS true)
	set(CMAKE_MACOSX_RPATH 0)
	set(compile_os "Darwin")
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
	add_definitions("-DUNIX")
	set(UNIX_OS true)
	set(compile_os "FreeBSD")
elseif (CMAKE_SYSTEM_NAME MATCHES "CYGWIN")
	add_definitions("-DWINDOWS")
	set(WIN_OS true)
	set(compile_os "CYGWIN")
	add_definitions("-DWIN32")
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
	add_definitions("-DWINDOWS")
	add_definitions("-DUNICODE")
	add_definitions("-DWIN32")
	set(WIN_OS true)
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
	add_definitions("-DOHOS")
	set(UNIX_OS true)
	set(OHOS_OS "ture")
	set(compile_os "Ohos")
	message("-- OHOS Cpu: ${OHOS_ARCH}")
else()
	message(FATAL_ERROR "unknown CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
endif()


if (CMAKE_SYSTEM_NAME MATCHES "Android")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
	link_directories(${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
	message("-- Android: LibraryProject-Project: ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI}")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	link_directories(${PROJECT_SOURCE_DIR}/libs/)
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	link_directories(${PROJECT_SOURCE_DIR}/libs/)
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	link_directories(${PROJECT_SOURCE_DIR}/libs/)
elseif (CMAKE_SYSTEM_NAME MATCHES "CYGWIN")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	link_directories(${PROJECT_SOURCE_DIR}/libs/)
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/)
	link_directories(${PROJECT_SOURCE_DIR}/libs/)
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/${OHOS_ARCH})
	set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/libs/${OHOS_ARCH})
	link_directories(${PROJECT_SOURCE_DIR}/libs/${OHOS_ARCH})
	message("-- OHOS: LibraryProject-Project: ${PROJECT_SOURCE_DIR}/libs/${OHOS_ARCH}")
else()
	message(FATAL_ERROR "unknown CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
endif()



if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	message("-- CPU-Target: 64")
elseif (Target64Bit STREQUAL true)
	message("-- CPU-Target: 32")
else()
	message(FATAL_ERROR "Bit = ${CMAKE_SIZEOF_VOID_P}")
endif()

SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

if(CMAKE_BUILD_TYPE STREQUAL Debug)
	set(CMAKE_BUILD_TYPE Debug)
	add_definitions(-DDEBUG)
	set(compile "-g3 -fPIC -fvisibility=hidden")
	set(static_compile "-g3 -fPIC -fvisibility=hidden")
	message("-- Debug Compile Project")
else()
   	set(CMAKE_BUILD_TYPE Release)
	set(compile "-O3 -g3 -fPIC -fvisibility=hidden")
	set(static_compile "-g3 -fPIC -fvisibility=hidden")
	message("-- Release Compile Project")
endif()

set(by3rd_library_output "${PROJECT_SOURCE_DIR}/3rd/lib" CACHE INTERNAL "3rd Library" )
set(by3rd_library_include "${PROJECT_SOURCE_DIR}/3rd/include" CACHE INTERNAL "3rd include" )


set(dynamic "-fvisibility=hidden -Wl,--no-as-needed -frtti -Wall -Werror -fexceptions")
set(execute "-Wall -Werror -fexceptions")
set(static  "-fPIC -fvisibility=hidden -Wl,--no-as-needed -frtti -Wall -Werror -fexceptions")

message("-- base-include: ${PROJECT_SOURCE_DIR}/include/")
message("-- base-include: ${by3rd_library_include}/")
message("-- base-lib: ${PROJECT_SOURCE_DIR}/libs/${CMAKE_BUILD_TYPE}/")
message("-- base-lib: ${PROJECT_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}/")
message("-- Compile: ${CMAKE_BUILD_TYPE}")
message("-- Compile param: ${compile}")
message("-- System: ${CMAKE_SYSTEM_NAME}")
message("-- System_Info_file: ${CMAKE_SYSTEM_INFO_FILE}")
message("-- System_Processor: ${CMAKE_SYSTEM_PROCESSOR}")
message("-- Host_System_Processor: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
message("-- System: ${CMAKE_SYSTEM}")
message("-- OutPut-Path: ${PROJECT_BINARY_DIR}")
message("-- OutPut-Project: ${EXECUTABLE_OUTPUT_PATH}")
message("-- OutPut-LibraryProject: ${LIBRARY_OUTPUT_PATH}")


add_subdirectory(stdcrt)
add_subdirectory(nettls)
add_subdirectory(stdcrt_test)
add_subdirectory(main)
add_subdirectory(testmain)
add_subdirectory(guidgen)
add_subdirectory(container)
add_subdirectory(mempool)
add_subdirectory(render)
add_subdirectory(res)
#add_subdirectory(uibase)
add_subdirectory(msgbus)
add_subdirectory(logs)
add_subdirectory(mainview)
add_subdirectory(net)
add_subdirectory(asynio)
add_subdirectory(license)
add_subdirectory(access)

if (CMAKE_SYSTEM_NAME MATCHES "Android")
	add_subdirectory(dispatch)
elseif (CMAKE_SYSTEM_NAME MATCHES "OHOS")
	add_subdirectory(dispatch)
else()

endif()






